import allure
import jsonpath
import requests

from src.api.base_api import BaseApi

# 完成send的调试，后面直接用就行，哦，还有一点需要再实体类上先继承BaseApi，然后初始化父类，然后再实例化时提供baseurl参数：比如： author = Author(base_url='https://ck-medium-api-test.cds8.cn/v1/')
from src.utils.data_model import data_model_list


class Author(BaseApi):

    def __init__(self, base_url, role=None):
        super().__init__(base_url, role)

    @allure.step("我的达人,查找对应达人")
    def medium_list(self, platform, **kwargs):
        '''
        达人管理里页面展示的list列表，其中包含有中介信息比如带建联等
        https://ck-medium-api-test.cds8.cn/v1/author/medium/list_new
        带有查询关键字：
        https://ck-medium-api-test.cds8.cn/v1/author/medium/list_new?keyword=KeleMuaaa
        返回一个响应体，里面有关于中介状态的状态码
        res.data.data.list.medium_info(此处要通过nick_name或user_id来确定跟进的用户).cooperate_status
        '''
        url = 'https://ck-medium-api-test.cds8.cn/v1/author/medium/list_new'
        params = {
            'platform': platform,
        }
        for k, v in kwargs.items():
            params[k] = v

        params_model = data_model_list.model_validate(params)

        params = params_model.model_dump()
        # params_model = data_model_list(**params)

        json_res = self.send(method='get', path='author/medium/list_new', params=params)
        return json_res

    @allure.step("达人库查询可跟进达人")
    def list(self, platform='dy', **kwargs):

        '''
        达人库中展示的达人列表信息，展示重点在于达人本身
        https://ck-medium-api-test.cds8.cn/v1/author/list
        可输入关键字进行查询

        '''

        params = {
            'platform': platform
        }
        for k, v in kwargs.items():
            params[k] = v

        params_model = data_model_list.model_validate(params)
        params = params_model.model_dump()
        data_res = self.send(method='get', path='author/list', params=params)
        print()
        return data_res

    @allure.step("达人选择下拉框")
    def list_dropdown_cooperate(self, user_id=34641, platform='dy', data_type=""):
        '''
        在进入表单页面，用于下拉框展示的达人数据列表
        url = 'https://ck-medium-api-test.cds8.cn/v1/author/medium/list_dropdown'
        '''

        params = {
            'data_type': data_type,
            'user_id': user_id,
            'platform': platform,
            'size': 100
        }
        json_res = self.send(method='get', path='author/medium/list_dropdown', params=params)
        print()
        return json_res

    @allure.step("跟进")
    def bind(self, author_id, platform='dy', from_source=5):
        '''
        跟进达人功能，类似于将某个达人纳入自己的业务中，
        即承接某位达人的交接任务
        https://ck-medium-api-test.cds8.cn/v1/commerce/author/medium/bind
        请求凭证：
        Authorization：Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmVfdGltZSI6MTcxNzg4MDQwMCwiaWF0IjoxNzE1MzM1NzYzLCJraWQiOiJDQVMtREhCMDI0VFBQN05LLVFYNFUwNiIsIm9wZXJhdGVfaWQiOiIwIiwicmlkIjoxLCJ1aWQiOiI2OWRiYWIwNS03MjY2LTQ4MjYtOGNkMC02ODQ2ODhlNTZmNjcifQ.0KTEYl9Wjo97P0xt-7aY-mWVQm_k2CoO9yy-Hz7SxP0
        参数表：
        author_id:str 跟进的达人uid
        platform:str 达人所在平台
        user_id:number 用户id，不填则会分配给自己
        from:number 跟进来源
        来源参考：
        MediumAuthorDistributeSea         = 3  //达人公海
        MediumAuthorDistributeMy          = 4  //我的达人
        MediumAuthorDistributeLibrary     = 5  //达人库
        MediumAuthorDistributeProduct     = 6  //商品找达人
        MediumAuthorDistributeBrand       = 7  //品牌找达人
        MediumAuthorDistributeClass       = 8  //品类找达人
        返回：
        code
        mesage

        '''
        url = 'https://ck-medium-api-test.cds8.cn/v1/commerce/author/medium/bind'
        data = {
            "author_id": [
                author_id
            ],
            "user_id": "",
            "platform": platform,
            "classify": 1,
            "from": from_source
        }
        json_res = self.send(method='post', path='author/medium/bind', json=data)
        # res = requests.post(url=url, json=data, headers=self.headers)
        print()
        return json_res

    @allure.step("释放达人，返回公海")
    def unbind_back_sea(self, user_id=34641, author_id='352280240724749', platform='dy'):
        '''
        释放达人返回公海
        https://ck-medium-api-test.cds8.cn/v1/author/medium/return/single
        负载：{author_id: "352280240724749", user_id: 34641, platform: "dy"}
        响应体：code：0   message：success
        '''
        url = 'https://ck-medium-api-test.cds8.cn/v1/author/medium/return/single'
        data = {
            'author_id': author_id,
            'user_id': user_id,
            'platform': platform
        }
        json_res = self.send(method='post', path='author/medium/return/single', json=data)
        print()
        return json_res

    @allure.step("与达人建联")
    def connect(self, author_id='3570818445221396', platform='dy', wechat_val='111'):
        url = 'https://ck-medium-api-test.cds8.cn/v1/author/update/connect'
        json_data = {
            "author_id": author_id,
            "platform": platform,
            "other_contact": [
                {
                    "key": "wechat",
                    "val": wechat_val
                }
            ]
        }
        json_res = self.send(method='post', path='author/update/connect', json=json_data)
        print()
        return json_res

    @allure.step('添加白名单')
    def add_white_list(self, list_author_id, platform='dy'):
        '''
        该接口是添加白名单
        url=https://ck-medium-api-test.cds8.cn/v1/author/whitelist/add
        post
        {
             "platform": "dy",
             "author_id": author_id
         }
        '''
        data = {
            "platform": platform,
            "author_id": list_author_id
        }
        res_json = self.send(method='POST', json=data, path='author/whitelist/add')
        return res_json

    def list_with_follower(self, keyword='milanjia830', platform='dy'):
        '''
        该接口的功能是在添加白名单时，点击的下拉框达人数据表
        url=https://ck-medium-api-test.cds8.cn/v1/author/list_with_follower
        get

        '''
        params = {
            'keyword': keyword,
            'platform': platform
        }
        res_json = self.send(method='get', path='author/list_with_follower', params=params)

        return res_json

    @allure.step('列出白名单列表')
    def get_white_list(self, keyword='milanjia830', platform='dy'):
        '''
        这个接口是用于白名单页面的查找列表
        url = https://ck-medium-api-test.cds8.cn/v1/author/whitelist
        get

        '''
        params = {
            'keyword': keyword,
            'platform': platform
        }
        res_json = self.send(method='get', path='author/whitelist', params=params)
        return res_json

    @allure.step('从白名单中移除')
    def del_from_white_list(self, platform='dy', author_id='3570818445221396'):
        '''
        该接口是从白名单中删除达人
        url=https://ck-medium-api-test.cds8.cn/v1/author/whitelist/del
        post

        '''
        json_data = {
            "platform": platform,
            "author_id": author_id
        }
        res_json = self.send(method='post', path='author/whitelist/del', json=json_data)

        return res_json

    @allure.step('添加黑名单')
    def add_black_list(self, reason='', author_id='3570818445221396', platform='dy', is_force=0):
        '''
        该接口为添加黑名单，在我的达人页面里进行拉黑操作
        url=https://ck-medium-api-test.cds8.cn/v1/author/medium/add/blacklist/single
        post
        '''
        json = {
            "author_id": author_id,
            "reason": reason,
            "platform": platform,
            "classify": 1,
            "images": [],
            "is_force": is_force
        }
        res_json = self.send(method='post', path='author/medium/add/blacklist/single', json=json)
        return res_json

    @allure.step('获取黑名单列表')
    def get_black_list(self, keyword='milanjia830', order_by=2, classify=1, platform='dy'):
        '''
        该接口是为了获取黑名单列表数据
        url=https://ck-medium-api-test.cds8.cn/v1/author/blacklist/list
        get
        '''
        params = {
            'keyword': keyword,
            'order_by': order_by,
            'classify': classify,
            'platform': platform,

        }
        res_json = self.send(method='get', path='author/blacklist/list', params=params)

        return res_json

    @allure.step('移除黑名单')
    def remove_black_list(self, author_id, reason='xxxx', platform='dy', classify=1, user_id=34641, recover_position=1):
        '''
        该接口的主要功能在于移除黑名单中的达人
        url=https://ck-medium-api-test.cds8.cn/v1/author/medium/remove/blacklist/single
        post
        参数说明：
        author_id	string
        非必须
        达人
        platform	string
        非必须
        平台,dy:抖音,xhs:小红书
        classify	number
        非必须
        业务,1带货,2内容
        user_id	number
        非必须
        指派媒介
        recover_position	number
        非必须
        将达人恢复的位置  1 恢复到达人公海 2 恢复后指派给媒介
        reason	string
        非必须
        原因
        '''
        json = {
            "author_id": author_id,
            "reason": reason,
            "platform": platform,
            "classify": classify,
            "user_id": user_id,
            "recover_position": recover_position
        }
        res_json = self.send(method='post', path='author/medium/remove/blacklist/single', json=json)
        return res_json

    @allure.step('在达人公海里搜寻达人')
    def list_corp_sea_author(self, keyword='milanjia830', order_by=2, classify=1, platform='dy', sort='volume30'):
        '''
        该接口主要在达人管理-达人公海页面进行数据搜索
        url=https://ck-medium-api-test.cds8.cn/v1/author/corp/list
        get
        '''
        params = {
            'keyword': keyword,
            'order_by': order_by,
            'classify': classify,
            'platform': platform,
            'sort': sort,
            'label': '',
            'product_category': '',
            'bring_product_type': -1,
            'author_reputation_level': -1,
            'product_price': -1,
            'fans_age': '',
            'fans_gender': -1,
            'fans_province': '',
            'live_fans_buy_price': '',
            'aweme_fans_buy_price': '',
            'live_buy_label': '',
            'aweme_buy_label': '',
            'authors_gender': -1,
            'authors_age': '',
            'authors_province': '',
            'is_have_contact': -1,
            'live_sale': '',
            'follower_count': '',
            'total_amount': '',
            'tag_id': '',
            'is_release': -1,
            'tag_type': 0,
            'liked_collected_count': '',
            'author_id': '',
            'page': 1,
            'size': 20,

        }
        res_json = self.send(method='get', path='author/corp/list', params=params)
        return res_json

    @allure.step("合作单创建达人选择下拉框")
    def medium_list_dropdown_cooperate(self, data_type="self", platform='dy'):
        """
        url=https://ck-medium-api-test.cds8.cn/v1/author/medium/list_dropdown_cooperate
        GET
        参数:
        data_type: 默认空(包含子部门达人),self(自己的达人)
        platform:
        """
        params = {
            "data_type": data_type,
            "platform": platform,
            "page": 1,
            "size": 50,
        }
        json_res = self.send(method='GET', path='author/medium/list_dropdown_cooperate', params=params)
        return json_res
